﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels
{
    public class AddCollectArExtendViewModel:ViewModelBase
    {
        public static GWCmdMakeUp GWCmdMakeUp;
        public static GWCmdMakeUpPlan GWCmdMakeUpPlan;
        private string address;

		public string Address
		{
			get { return address; }
			set { address = value; RaisePropertyChanged(); }
		}

		private string asset;

		public string Asset
		{
			get { return asset; }
			set { asset = value; RaisePropertyChanged(); }
		}

		private string pt;

		public string PT
		{
			get { return pt; }
			set { pt = value; RaisePropertyChanged(); }
		}

		private string ct;

		public string CT
		{
			get { return ct; }
			set { ct = value; RaisePropertyChanged(); }
		}

        public AddCollectArExtendViewModel()
        {
            string[] datas;
            if (MainViewModel.TopIndex == 0)
            {
                GWCmdMakeUp = ServiceLocator.Current.GetInstance<GWCmdMakeUp>();
                if (GWCmdMakeUp.SubItemName == null)
                {
                    return;
                }
                datas = GWCmdMakeUp.DataValue.Split(',');
            }
            else
            {
                GWCmdMakeUpPlan = ServiceLocator.Current.GetInstance<GWCmdMakeUpPlan>();
                if (GWCmdMakeUpPlan.SubItemName == null)
                {
                    return;
                }
                datas = GWCmdMakeUpPlan.DataValue.Split(',');
            }
            
            Address = datas[0];
            Asset = datas[1];
            PT = datas[2];
            CT = datas[3];
        }

        public RelayCommand<Window> OKCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.Append(Address);
                    stringBuilder.Append(",");
                    stringBuilder.Append(Asset);
                    stringBuilder.Append(",");
                    stringBuilder.Append(PT);
                    stringBuilder.Append(",");
                    stringBuilder.Append(CT);
                    if (MainViewModel.TopIndex == 0)
                    {
                        GWCmdMakeUp.DataValue = stringBuilder.ToString();
                    }
                    else
                    {
                        GWCmdMakeUpPlan.DataValue = stringBuilder.ToString();
                    }
                    
                    w.DialogResult = true;
                });
                return command;
            }
        }

        public RelayCommand<Window> CloseCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    w.DialogResult = false;
                });
                return command;
            }
        }
    }
}
